//
//  HRStatusViewModel.swift
//  microBlog
//
//  Created by HR_W on 16/5/25.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit

class HRStatusViewModel: NSObject {

    var status :HRStatus?
    
    var repostsText: String?
    var commentText: String?
    var attitudesText: String?
    
    var mbrankImage: UIImage?
    var veritiedImage: UIImage?
    var headImageURL: NSURL?
    
    var sourceText: String?
    
    var time: String? {
    
    return NSDate.sinaDate(status?.created_at ?? "")?.fullDescription()
    
    }
    
    
    
    init(status: HRStatus) {
        super.init()
        
        self.status = status
        //处理用户头像地址
        headImageURL = NSURL(string: status.user?.profile_image_url ?? "")
        //处理用户等级的图片
        dealMbrankImage()
        //处理认证类型的图片
        dealVeitiedImage()
        //微博来源
        dealSoucreText()
        commentText = dealToolBarText(status.comments_count, defaultText: "评论")
        repostsText = dealToolBarText(status.reposts_count, defaultText: "转发")
        attitudesText = dealToolBarText(status.attitudes_count, defaultText: "攒")
    }
    
    
}


extension HRStatusViewModel {
    
    private func  dealToolBarText(count: Int,defaultText: String) -> String {
        if count <= 0 {
            return defaultText
        }
        if count >= 1000 {
            return"\(Double(count / 1000) / 10)万"
            
        }
    
    
        return "\(count)"
    }
    
    
    
    //截取字符串
    private func dealSoucreText() {
    
        let startFlag = "\">"
        let endFlag = "</a>"
        let str = status?.source ?? ""
        let startRange = str.rangeOfString(startFlag)
        let endRange = str.rangeOfString(endFlag)
        if let start = startRange, end = endRange {
            let range = start.endIndex..<end.startIndex
            sourceText = str.substringWithRange(range)
            
        }
  
    }

    private func dealMbrankImage() {
    
        let mbType = status?.user?.mbrank ?? 0
        
        if mbType > 0 && mbType < 7 {
           
          mbrankImage = UIImage(named: "common_icon_membership_level\(mbType)" )
            
        }
    }
    
    private func dealVeitiedImage() {
        
        let veriteidType = status?.user?.verified ?? 0
        
        if veriteidType == 1 {
            veritiedImage = UIImage(named: "avatar_vip")
        }
        
    
    }


}